package com.hr.grpc;

import io.grpc.stub.StreamObserver;
import seaway.AudioServiceGrpc;
import seaway.AudioServiceOuterClass;

public class HelloGrpServer extends AudioServiceGrpc.AudioServiceImplBase {
    @Override
    public StreamObserver<AudioServiceOuterClass.AudioRequest> sendAudio(
            StreamObserver<seaway.AudioServiceOuterClass.AudioResponse> responseObserver) {
        return new StreamObserver<AudioServiceOuterClass.AudioRequest>() {

            @Override
            public void onNext(AudioServiceOuterClass.AudioRequest audioRequest) {
                System.out.println(responseObserver.toString());
                System.out.println(Thread.currentThread().getName() + " 服务端接收到客户端提交的消息:" + audioRequest.getResult());
                responseObserver.onNext(AudioServiceOuterClass.AudioResponse.newBuilder()
                        .setResult(audioRequest.getResult()).build());
                responseObserver.onCompleted();
            }

            @Override
            public void onError(Throwable throwable) {
                System.out.println(Thread.currentThread().getName() + " 服务端异常:" + throwable.getMessage());
            }

            @Override
            public void onCompleted() {
                System.out.println(Thread.currentThread().getName() + " 服务端接收到了所有的请求消息.....");

            }
        };
    }
}
